package com.secsys.action.finance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.secsys.action.BaseAction;
import com.secsys.dto.AssistDto;
import com.secsys.dto.StoPriceearningDTO;
import com.secsys.dto.StoStockDTO;
import com.secsys.service.StoPriceearningService;
import com.secsys.service.StoStockService;

/**
 * 对应表tb_sto_ priceearning (每股收益表)相关数据的action
 * <pre>
 * 1.处理功能（财务分析里面的）:
 * (1).每股收益：preparePerProfit、perProfit、perProfitDetail、doPerProfit
 * 分别为默认处理、用户搜索、详细信息、内部协助函数。
 *@作者 tiger.hu.tiger@gmail.com
 *@创建日期 2011-5-8
 *@版本1.0
 */
public class PriceEarningAction extends BaseAction<StoPriceearningDTO> {
	private static final long serialVersionUID = -3645574123965205125L;
	private final int limit=10; 
	private List<StoPriceearningDTO> stoPriceearningDTOList;
	private List<StoPriceearningDTO> oldYearStoPriceearningDTOList;
	private StoPriceearningDTO stoPriceearningDTO;
	
	private List<StoStockDTO> stockDTOList;
	private AssistDto assistDto;
	private StoStockDTO stockDTO;
	private StoStockService stoStockService;
	
	private StoPriceearningService stoPriceearningService;
	private Logger logger=Logger.getLogger(PriceEarningAction.class);
	
	/**
	 * 财务分析之“每股收益”准备action（默认条件下的查询,比如：每年上升20%），对应表：tb_sto_priceearning
	 * @return	
	 * 			返回默认条件下的查询结果
	 * @throws Exception
	 */
	public String preparePerProfit() throws Exception{

		this.assistDto=new AssistDto();
//		assistDto.setStartDate(DateFormatUtils.convertStringToDate("2009-12-31"));
//		assistDto.setEndDate(DateFormatUtils.convertStringToDate("2010-12-31"));
		assistDto.setStartYear("2009");
		assistDto.setEndYear("2010");
		assistDto.setRate(15);
		assistDto.setUpdown(1);
		logger.info("setStartYear="+assistDto.getStartYear()+";setEndYear="+assistDto.getEndYear()+"preparePerProfit()"); 
		this.doPerProfit();
		
		return "search";
	}
	
	/**
	 * <pre>财务分析之“每股收益”;处理用户自定义条件的查询action；查询条件有：wholeYear的差异，fieldYear的差异，即：
	 * 1.每几年的时间段，
	 * 2.上升或下降的幅度
	 * 3.对应表：tb_sto_priceearning</pre>
	 * @return
	 * 			返回用户自定义条件的查询结果
	 * @throws Exception
	 */
	public String perProfit() throws Exception{
		if (!this.doPerProfit()) {
			return "prepare";
		}
		
		return "search";
	}
	
	public String perProfitDetail() throws Exception{
		this.stoPriceearningDTOList=stoPriceearningService.queryStoPriceearningByStockCode(stockDTO.getStockCode());
		stockDTO=stoStockService.findByStockCode(stockDTO.getStockCode());
		logger.info("stockDTO="+stockDTO.getStockCode()+" "+stockDTO.getStockName()+" in perProfitDetail()"); 
		return "detail";
	}
	
	private boolean doPerProfit(){
//		String startYear=String.valueOf(assistDto.getStartDate().getYear()+1900);
//		String endYear=String.valueOf(assistDto.getEndDate().getYear()+1900);
		String startYear=assistDto.getStartYear();
		String endYear=assistDto.getEndYear();
		double rate=assistDto.getRate();
		int updown=assistDto.getUpdown();
		logger.info("开始时间="+startYear+";结束时间="+endYear+";百分比="+rate+";上升还是下降="+updown);
		/**
		 * 设置当前页
		 */
		if (0 == assistDto.getCurrentPages()) {
			assistDto.setCurrentPages(1);
		}
		/**
		 * 偏移值= (当前第几页-1)*每页个数
		 */
		int offset = (assistDto.getCurrentPages() - 1) * limit;
		/**
		 * 查询得到的总记录数
		 */
		int num=stoPriceearningService.countStoPriceearningBySearch(startYear, endYear, rate,updown);
		logger.info("--查询得到的总记录数-----"+num+"-----");
		/**
		 * 总共多少页
		 */
		int totalPages = (num / limit);
		if (num % limit > 0) {
			totalPages++;
		}
		if (assistDto.getCurrentPages() >totalPages) {
			assistDto.setCurrentPages(1); 
			return false;
		}
		assistDto.setTotalPages(totalPages);
		
		HashMap<String,List> map=stoPriceearningService.queryStoPriceearningBySearch(startYear, endYear, rate,updown,offset,limit); 
		this.stoPriceearningDTOList=map.get("stoPriceearningDTOList");
		this.stockDTOList=map.get("stockDTOList");
		this.oldYearStoPriceearningDTOList=map.get("oldYearStoPriceearningDTOList");
		
//		for (StoPriceearningDTO stoPriceearningDTO : stoPriceearningDTOList) {
//			System.out.println(stoPriceearningDTO.getId()+"-stoPriceearningDTOList--"+stoPriceearningDTO.getFieldYear());
//		}
		
		logger.info("总数："+assistDto.getTotalPages()+";当前"+assistDto.getCurrentPages()); 
		return true;
	}
	
	
	
	
	
	public PriceEarningAction(){
		if (null ==this.assistDto) {
			this.assistDto=new AssistDto();
		}
		if (null ==this.stockDTO) {
			this.stockDTO=new StoStockDTO();
		}
		if (null ==this.stockDTOList) {
			this.stockDTOList=new ArrayList<StoStockDTO>();
		}
		if (null ==this.stoPriceearningDTOList) {
			this.stoPriceearningDTOList=new ArrayList<StoPriceearningDTO>();
		}
		if (null ==this.oldYearStoPriceearningDTOList) {
			this.oldYearStoPriceearningDTOList=new ArrayList<StoPriceearningDTO>();
		}
	}
	
	@Override
	public StoPriceearningDTO getModel() {
		if (null ==stoPriceearningDTO ) {
			stoPriceearningDTO=new StoPriceearningDTO();
		}
		return stoPriceearningDTO;
	}

	public AssistDto getAssistDto() {
		return assistDto;
	}

	public void setAssistDto(AssistDto assistDto) {
		this.assistDto = assistDto;
	}

	public StoPriceearningDTO getStoPriceearningDTO() {
		return stoPriceearningDTO;
	}

	public void setStoPriceearningDTO(StoPriceearningDTO stoPriceearningDTO) {
		this.stoPriceearningDTO = stoPriceearningDTO;
	}

	public StoStockDTO getStockDTO() {
		return stockDTO;
	}

	public void setStockDTO(StoStockDTO stockDTO) {
		this.stockDTO = stockDTO;
	}

	public StoStockService getStoStockService() {
		return stoStockService;
	}

	public void setStoStockService(StoStockService stoStockService) {
		this.stoStockService = stoStockService;
	}

	public StoPriceearningService getStoPriceearningService() {
		return stoPriceearningService;
	}

	public void setStoPriceearningService(
			StoPriceearningService stoPriceearningService) {
		this.stoPriceearningService = stoPriceearningService;
	}

	public List<StoStockDTO> getStockDTOList() {
		return stockDTOList;
	}

	public void setStockDTOList(List<StoStockDTO> stockDTOList) {
		this.stockDTOList = stockDTOList;
	}

	public List<StoPriceearningDTO> getStoPriceearningDTOList() {
		return stoPriceearningDTOList;
	}

	public void setStoPriceearningDTOList(
			List<StoPriceearningDTO> stoPriceearningDTOList) {
		this.stoPriceearningDTOList = stoPriceearningDTOList;
	}

	public List<StoPriceearningDTO> getOldYearStoPriceearningDTOList() {
		return oldYearStoPriceearningDTOList;
	}

	public void setOldYearStoPriceearningDTOList(
			List<StoPriceearningDTO> oldYearStoPriceearningDTOList) {
		this.oldYearStoPriceearningDTOList = oldYearStoPriceearningDTOList;
	}
	
}
